package SignAndRegister;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class RegisterWindow extends JFrame {

    public java.net.URL trueIconURL = RegisterWindow.class.getClassLoader().getResource("image/true.jpg");
    public ImageIcon trueImg = new ImageIcon(trueIconURL);
    public JLabel trueIcon = new JLabel(trueImg);

    public java.net.URL falseIconURL = RegisterWindow.class.getClassLoader().getResource("image/false.jpg");
    public ImageIcon falseImg = new ImageIcon(falseIconURL);
    public JLabel falseIcon = new JLabel(falseImg);


    private JButton registerButtom = new JButton("确认注册");
    private JTextField iDText = new JTextField();
    private JTextField codeText = new JTextField();
    private JComboBox typeChoice = new JComboBox<>();

    JPanel registerPanel = new JPanel();

    private RegisterWindowHandler registerWindowHandler = new RegisterWindowHandler(this);

    public RegisterWindow() {

        this.add(registerPanel);
        registerPanel.setLayout(null);
        placeComponents(registerPanel);

        iDText.getDocument().addDocumentListener(registerWindowHandler);  //理清DocumentListener和AcitonListener的关系
        registerButtom.setActionCommand("confirmRegister");
        registerButtom.addActionListener(registerWindowHandler);
        typeChoice.setActionCommand("changeAccountType");
        typeChoice.addActionListener(registerWindowHandler);

        //限制非法输入
        iDText.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                int keyChar = e.getKeyChar();
                if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){

                }else{
                    e.consume();
                }
            }
        });

        Font defaultFont = registerPanel.getFont();
        Font font = new Font(defaultFont.getFamily(), defaultFont.getStyle(), 100);
        this.setFont(font);

        //设置登录界面的一些参数：背景颜色、标题、大小、位置、是否可调大小、是否可见
        this.setBackground(Color.white);
        setTitle("注册界面");
        setBounds(0, 0, 600, 430);
        setLocationRelativeTo(null);//居中显示
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  //用户单击窗口的关闭按钮时程序执行的操作
        setResizable(false);
        setVisible(true);

    }

    private void placeComponents(JPanel registerPanel){

        int labelDivWidth = 160;
        int labelDivHeight = 120;

        registerPanel.setLayout(null);

        JLabel iDLabel = new JLabel("账户ID:");
        JLabel codeLabel = new JLabel("账户密码:");
        JLabel typeLabel = new JLabel("账户类型:");

        typeChoice.addItem("住客");
        typeChoice.addItem("职工");
        typeChoice.addItem("酒店管理员");
        typeChoice.addItem("系统管理员");


        iDLabel.setBounds(labelDivWidth,labelDivHeight+20,80,25);
        iDText.setBounds(labelDivWidth+70,labelDivHeight+20,165,25);
        codeLabel.setBounds(labelDivWidth,labelDivHeight+50,80,25);
        codeText.setBounds(labelDivWidth+70,labelDivHeight+50,165,25);
        typeLabel.setBounds(labelDivWidth,labelDivHeight+80,80,25);
        typeChoice.setBounds(labelDivWidth+70,labelDivHeight+80,165,25);
        registerButtom.setBounds(labelDivWidth,labelDivHeight+110,235,25);

        trueImg.setImage(trueImg.getImage().getScaledInstance(15, 15, Image.SCALE_DEFAULT));
        trueIcon.setBounds(labelDivWidth+190,labelDivHeight-29,120,120);
        falseImg.setImage(falseImg.getImage().getScaledInstance(15, 15, Image.SCALE_DEFAULT));
        falseIcon.setBounds(labelDivWidth+190,labelDivHeight-29,120,120);

        registerPanel.add(iDLabel);
        registerPanel.add(iDText);
        registerPanel.add(codeLabel);
        registerPanel.add(codeText);
        registerPanel.add(typeLabel);
        registerPanel.add(typeChoice);
        registerPanel.add(registerButtom);
        registerPanel.add(trueIcon);
        registerPanel.add(falseIcon);

        trueIcon.setVisible(false);
        falseIcon.setVisible(false);

    }

    public String getIDText(){
        return iDText.getText();
    }

    public String getCodeText(){
        return codeText.getText();
    }

    public String getTypeChoice(){
        return typeChoice.getSelectedItem().toString();
    }

    public void changeTrueIcon(){
        trueIcon.setVisible(true);
        falseIcon.setVisible(false);
    }

    public void changeFalseIcon(){
        trueIcon.setVisible(false);
        falseIcon.setVisible(true);
    }

}
